package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.pojo.dto.PostQueryDto;
import com.example.pojo.entity.Post;
import com.example.pojo.vo.PostQueryVo;
import com.example.service.PostService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 帖子 controller 层
 */
@RestController
@RequestMapping("/post")
public class PostController {

    @Resource
    private PostService postService;

    @PostMapping("/create")
    public Result createPost(@RequestBody Post post) {
        this.postService.createPost(post);
        return Result.success();
    }

    @PostMapping("/pageQuery")
    public Result pageQuery(@RequestBody PostQueryDto dto) {
        PageResult<PostQueryVo> pageResult = this.postService.pageQuery(dto);
        return Result.success(pageResult);
    }

    @PostMapping("/pageQuery/person")
    public Result pageQueryPersonPost(@RequestBody PostQueryDto dto) {
        PageResult<PostQueryVo> pageResult = this.postService.pageQueryPerson(dto);
        return Result.success(pageResult);
    }

    @PostMapping("/pageQuery/forum")
    public Result pageQueryForumPost(@RequestBody PostQueryDto dto) {
        PageResult<PostQueryVo> pageResult = this.postService.pageQueryForumPost(dto);
        return Result.success(pageResult);
    }

    @GetMapping("/query/detail")
    public Result queryPostDetail(@RequestParam Integer id) {
        PostQueryVo vo = this.postService.queryPostDetail(id);
        return Result.success(vo);
    }

    @PutMapping("/update/view")
    public Result addPostViewCount(@RequestParam Integer id) {
        this.postService.addPostViewCount(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateById(@RequestBody Post post) {
        this.postService.update(post);
        return Result.success();
    }

    @PutMapping("/audit")
    public Result auditPost(@RequestBody Post post) {
        this.postService.auditPost(post);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        this.postService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result delete(@RequestBody List<Integer> ids) {
        this.postService.deleteBatch(ids);
        return Result.success();
    }
}
